package com.txcb.lib.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * Created by zhangyuncai on 2018/9/11.
 */
public class SharedPreferencesUtils {

    private static final String SP_NAME = "txcb";
    private static final int SP_MODE = Context.MODE_PRIVATE;
    private static SharedPreferences sp;


    public static void initSharedPreferences(Context context) {
        if (sp == null && context != null) {
            sp = context.getApplicationContext().getSharedPreferences(SP_NAME, SP_MODE);
        }
    }


    /**
     * 保存boolean
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        initSharedPreferences(context);
        saveBoolean(key, value);
    }

    public static void saveBoolean(String key, boolean value) {
        if (sp != null)
            sp.edit().putBoolean(key, value).apply();
    }

    /**
     * 读取boolean
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean loadBoolean(Context context, String key) {
        initSharedPreferences(context);
        return loadBoolean(key);
    }

    public static boolean loadBoolean(String key) {
        if (sp != null)
            return sp.getBoolean(key, false);
        return true;
    }

    /**
     * 保存string
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveString(Context context, String key, String value) {
        initSharedPreferences(context);
        saveString(key, value);
    }

    public static void saveString(String key, String value) {
        if (sp != null)
            sp.edit().putString(key, value).apply();
    }


    public static void removeString(String key) {
        if (sp != null) {
            sp.edit().remove(key).apply();
        }
    }

    /**
     * 读取string
     *
     * @param context
     * @param key
     * @return
     */
    public static String loadString(Context context, String key) {
        initSharedPreferences(context);
        return loadString(key);
    }

    public static String loadString(String key) {
        if (sp != null)
            return sp.getString(key, "");
        return "";
    }

    /**
     * 保存int
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveint(Context context, String key, int value) {
        initSharedPreferences(context);
        saveint(key, value);
    }

    public static void saveint(String key, int value) {
        if (sp != null)
            sp.edit().putInt(key, value).apply();
    }

    /**
     * 读取int
     *
     * @param context
     * @param key
     * @return
     */
    public static int loadint(Context context, String key) {
        initSharedPreferences(context);
        return loadint(key);
    }

    public static int loadint(String key) {
        if (sp != null)
            return sp.getInt(key, 0);
        return 0;
    }


    /**
     * 将对象进行base64编码后保存到SharePref中
     *
     * @param key
     * @param object
     */
    public static void saveObject(Context context, final String key, final Object object){
        if (sp == null) {
            initSharedPreferences(context);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            // 将对象的转为base64码
            String objBase64 = new String(Base64.encodeBase64(baos
                    .toByteArray()));

            sp.edit().putString(key, objBase64).commit();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将SharePref中经过base64编码的对象读取出来
     *
     * @param key
     * @return
     */
    public  static Object getObj(Context context, String key) {
        if (sp == null) {
           initSharedPreferences(context);
        }
        String objBase64 = sp.getString(key, null);
        if (TextUtils.isEmpty(objBase64))
            return null;

        // 对Base64格式的字符串进行解码
        byte[] base64Bytes = Base64.decodeBase64(objBase64.getBytes());
        ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);

        ObjectInputStream ois;
        Object obj = null;
        try {
            ois = new ObjectInputStream(bais);
            obj = (Object) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

}
